package com.roc.zookeeper;

import java.util.List;
import java.util.concurrent.TimeUnit;

import org.I0Itec.zkclient.DataUpdater;
import org.I0Itec.zkclient.IZkChildListener;
import org.I0Itec.zkclient.IZkDataListener;
import org.I0Itec.zkclient.IZkStateListener;
import org.I0Itec.zkclient.ZkClient;
import org.apache.zookeeper.Watcher.Event.KeeperState;

/**
 * ZkClient的使用测试
 *
 * @author 单红宇(365384722)
 * @myblog http://blog.csdn.net/catoop/
 * @create 2016年3月11日
 */
public class ZkClientTest {
	
	
	private static final String ZK_SERVER = "192.168.1.156:2181,192.168.1.156:2182,192.168.1.156:2183";
	
	public static void main(String[] args) {
		
		
		ZkClient zkClient = new ZkClient(ZK_SERVER);
		String node = "/myapp";
		
		// 订阅监听事件
		childChangesListener(zkClient, node);
		dataChangesListener(zkClient, node);
		stateChangesListener(zkClient);
		
		if (!zkClient.exists(node)) {
			zkClient.createPersistent(node, "hello zookeeper");
		}
		
		System.out.println(zkClient.readData(node).toString());
		
		zkClient.updateDataSerialized(node, new DataUpdater<String>() {
			
			public String update(String currentData) {
				return currentData + "-123";
			}
		});
		
		System.out.println(zkClient.readData(node).toString());
		
		try {
			TimeUnit.SECONDS.sleep(3);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 订阅children变化
	 *
	 * @param zkClient
	 * @param path
	 */
	public static void childChangesListener(ZkClient zkClient, final String path) {
		zkClient.subscribeChildChanges(path, new IZkChildListener() {
			
			public void handleChildChange(String parentPath, List<String> currentChilds)
			        throws Exception {
				System.out.println("clildren of path " + parentPath + ":" + currentChilds);
			}
			
		});
	}
	
	/**
	 * 订阅节点数据变化
	 *
	 * @param zkClient
	 * @param path
	 */
	public static void dataChangesListener(ZkClient zkClient, final String path) {
		zkClient.subscribeDataChanges(path, new IZkDataListener() {
			
			public void handleDataChange(String dataPath, Object data) throws Exception {
				System.out.println("Data of " + dataPath + " has changed.");
			}
			
			public void handleDataDeleted(String dataPath) throws Exception {
				System.out.println("Data of " + dataPath + " has changed.");
			}
			
		});
	}
	
	/**
	 * 订阅状态变化
	 *
	 * @param zkClient
	 */
	public static void stateChangesListener(ZkClient zkClient) {
		zkClient.subscribeStateChanges(new IZkStateListener() {
			
			public void handleStateChanged(KeeperState state) throws Exception {
				System.out.println("handleStateChanged");
			}
			
			public void handleSessionEstablishmentError(Throwable error) throws Exception {
				System.out.println("handleSessionEstablishmentError");
			}
			
			public void handleNewSession() throws Exception {
				System.out.println("handleNewSession");
			}
		});
	}
}


